package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EsStockDamageReportProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EsStockDamageReportMapper;
import com.ruoyi.system.domain.EsStockDamageReport;
import com.ruoyi.system.service.IEsStockDamageReportService;

/**
 * 库存报损单(es_stock_damage_report)Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-14
 */
@Service
public class EsStockDamageReportServiceImpl implements IEsStockDamageReportService
{
    @Autowired
    private EsStockDamageReportMapper esStockDamageReportMapper;

    /**
     * 查询库存报损单(es_stock_damage_report)
     *
     * @param id 库存报损单(es_stock_damage_report)主键
     * @return 库存报损单(es_stock_damage_report)
     */
    @Override
    public EsStockDamageReport selectEsStockDamageReportById(Long id) {
        EsStockDamageReport report = esStockDamageReportMapper.selectEsStockDamageReportById(id);
        if (report != null) {
            report.setProducts(esStockDamageReportMapper.selectProductsByReportId(id));
        }
        return report;
    }

    /**
     * 查询库存报损单(es_stock_damage_report)列表
     *
     * @param esStockDamageReport 库存报损单(es_stock_damage_report)
     * @return 库存报损单(es_stock_damage_report)
     */
    @Override
    public List<EsStockDamageReport> selectEsStockDamageReportList(EsStockDamageReport esStockDamageReport)
    {
        return esStockDamageReportMapper.selectEsStockDamageReportList(esStockDamageReport);
    }

    /**
     * 新增库存报损单(es_stock_damage_report)
     *
     * @param esStockDamageReport 库存报损单(es_stock_damage_report)
     * @return 结果
     */
    @Override
    public int insertEsStockDamageReport(EsStockDamageReport esStockDamageReport) {
        esStockDamageReport.setCreateTime(DateUtils.getNowDate());
        int rows = esStockDamageReportMapper.insertEsStockDamageReport(esStockDamageReport);
        if (rows > 0 && esStockDamageReport.getProducts() != null) {
            insertProducts(esStockDamageReport);
        }
        return rows;
    }

    /**
     * 修改库存报损单(es_stock_damage_report)
     *
     * @param esStockDamageReport 库存报损单(es_stock_damage_report)
     * @return 结果
     */
    @Override
    public int updateEsStockDamageReport(EsStockDamageReport esStockDamageReport) {
        esStockDamageReportMapper.deleteProductsByReportId(esStockDamageReport.getId());
        if (esStockDamageReport.getProducts() != null) {
            insertProducts(esStockDamageReport);
        }
        return esStockDamageReportMapper.updateEsStockDamageReport(esStockDamageReport);
    }

    /**
     * 批量删除库存报损单(es_stock_damage_report)
     *
     * @param ids 需要删除的库存报损单(es_stock_damage_report)主键
     * @return 结果
     */
    @Override
    public int deleteEsStockDamageReportByIds(Long[] ids)
    {
        return esStockDamageReportMapper.deleteEsStockDamageReportByIds(ids);
    }

    /**
     * 删除库存报损单(es_stock_damage_report)信息
     *
     * @param id 库存报损单(es_stock_damage_report)主键
     * @return 结果
     */
    @Override
    public int deleteEsStockDamageReportById(Long id)
    {
        return esStockDamageReportMapper.deleteEsStockDamageReportById(id);
    }

    @Override
    public int submitEsStockDamageReport(Long id) {
        EsStockDamageReport report = esStockDamageReportMapper.selectEsStockDamageReportById(id);
        if (report == null) {
            throw new RuntimeException("报损单不存在");
        }
        report.setStatus(1L); // 设置为待审核状态
        return esStockDamageReportMapper.updateEsStockDamageReport(report);
    }

    @Override
    public int auditEsStockDamageReport(Long id) {
        EsStockDamageReport report = esStockDamageReportMapper.selectEsStockDamageReportById(id);
        if (report == null) {
            throw new RuntimeException("报损单不存在");
        }
        report.setStatus(2L); // 设置为审核通过状态
        return esStockDamageReportMapper.updateEsStockDamageReport(report);
    }

    @Override
    public List<Map<String, String>> getDamageTypes() {
        return esStockDamageReportMapper.selectDamageTypes();
    }

    @Override
    public List<Map<String, Object>> selectGoodsListWithStock(Long warehouseId, String keyword) {
        return esStockDamageReportMapper.selectGoodsListWithStock(warehouseId, keyword);
    }

    private void insertProducts(EsStockDamageReport esStockDamageReport) {
        List<EsStockDamageReportProduct> products = esStockDamageReport.getProducts();
        for (EsStockDamageReportProduct product : products) {
            product.setReportId(esStockDamageReport.getId());
            esStockDamageReportMapper.insertProduct(product);
        }
    }


}
